package cn.liberals.basic.controller;

import cn.liberals.basic.exception.BusinessException;
import cn.liberals.basic.service.IVerifyCodeService;
import cn.liberals.basic.utils.AjaxResult;
import cn.liberals.basic.vo.RegisterInEmailVo;
import cn.liberals.basic.vo.RegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /* 
    * @Title: graph
    * @Description: 获取图形验证码接口
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/9 20:10  
    * @Parameters: 
    * @Return 
    */
    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult graph(@PathVariable("imageCodeKey") String imageCodeKey) {
        try {
            String base64Str = verifyCodeService.getImageCode(imageCodeKey);
            return AjaxResult.me().setResultObj(base64Str);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /*
     * @Title: smsCode
     * @Description: 发送图形验证码
     * @Author: liberals
     * @Version: 1.0
     * @Date:  2022/7/6 15:45
     * @Parameters:
     * @Return
     */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody RegisterVo vo) {
        try {
            // 调用业务层的方法发送验证码
            verifyCodeService.smsCode(vo);
            // 返回信息
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /*
    * @Title: emailCode
    * @Description: 获取邮箱接口
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/9 20:11
    * @Parameters:
    * @Return
    */
    @PostMapping("/emailCode")
    public AjaxResult emailCode(@RequestBody RegisterInEmailVo registerInEmailVo){
        try {
            verifyCodeService.emailCode(registerInEmailVo);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    @PostMapping ("/mobileCode")
    public AjaxResult mobileCode(@RequestBody RegisterVo registerVo){
        try {
            verifyCodeService.mobileCode(registerVo);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }
}
